<template>
	<view class="follow">
		<view class="follow-tab">
			<view class="follow-tab__box">
				<view class="follow-tab__item" :class="{active: indexTab === 0}" @click="tab(0)">文章</view>
				<view class="follow-tab__item" :class="{active: indexTab === 1}" @click="tab(1)">作者</view>
			</view>
		</view>
		<view class="follow-list">
			<!-- 轮播图组件 statrt -->
			<swiper class="follow-list__swiper" :current="indexTab" @change="change">
				<swiper-item>
					<!-- 滚动组件 statr -->
					<list-scroll @pullMore="more">
						<!-- 卡片组件 start -->
						<list-card types="follow" v-for="item,index in followList" :item="item" :key="index"/>
						<!-- 卡片组件 end -->
						<!-- 加载组件 statr -->
						<uni-load-more :status="load[0].loading" iconType="snow"/>
						<!-- 加载组件 end -->
					</list-scroll>
					<!-- 滚动组件 end -->
				</swiper-item>
				<swiper-item>
					<!-- 滚动组件 statr -->
					<list-scroll @pullMore="more">
						<!-- 关注用户组件 start -->
						<list-author v-for="item,index in followAuthorList" :key="index" :item="item" />
						<!-- 关注用户组件 end -->
						<!-- 加载组件 statr -->
						<uni-load-more :status="load[1].loading" iconType="snow"/>
						<!-- 加载组件 end -->
					</list-scroll>
					<!-- 滚动组件 end -->
				</swiper-item>
			</swiper>
			<!-- 轮播图组件 end -->
		</view>
	</view>
</template>

<script>
	import {get_follow, get_author} from '@/common/api/follow.js';
	export default {
		data() {
			return {
				// 选项卡下标
				indexTab: 0,
				// 关注的文章
				followList: [],
				// 关注的作者
				followAuthorList: [],
				load: [],
				newLoad: false,
				newAuthorLoad: false
				
			}
		},
		onLoad() {
			uni.$on('update_follow', () => {
				this.newLoad = true
				this.getFollow()
			})
			
			uni.$on('update_follow_page', () => {
				this.newLoad = true
				this.load[0].page = 'loading'
				this.load[0].page = 1
				this.getFollow()
			})
			
			uni.$on('update_follow_author', () => {
				this.newAuthorLoad = true
				this.load[1].page = 'loading'
				this.load[1].page = 1
				this.getFollowAuthor()
			})
			
			this.$set(this.load, 0, {
				loading: 'loading',
				page: 1
			})
			
			this.$set(this.load, 1, {
				loading: 'loading',
				page: 1
			})
			
			this.getFollowAuthor()
			
			this.getFollow()
			
		},
		onShow() {
		},
		methods: {
			// tab切换
			tab(indexTab) {
				this.indexTab = indexTab
			},
			// 轮播图切换事件
			change(e) {
				// 获取轮播图的索引
				const current = e.detail.current
				this.indexTab = current
			},
			// 获取关注的文章
			getFollow() {
				
				this.load[0].loading = 'loading'
				get_follow({
					pageSize: 5,
					page: this.load[0].page
				}).then(res => {
					console.log('关注的文章', res)
					this.load[0].loading = 'more'
					
					if(!this.newLoad) {
						const oldData = this.followList || []
						const newData = res.data
						oldData.push(...newData)
						this.followList = oldData
					} else if (this.newLoad) {
						this.followList = res.data
					}
						
					// 判断是否有数据
					if (res.data.length < 5) {
						return this.load[0].loading = 'noMore'
					}
						
				})
			},
			// 获取关注的作者
			getFollowAuthor() {
				this.load[1].loading = 'loading'
				get_author({
					pageSize: 10,
					page: this.load[1].page
				}).then(res => {
					console.log('关注的作者', res)
					this.load[1].loading = 'more'
					
					if(!this.newAuthorLoad) {
						const oldData = this.followAuthorList || []
						const newData = res.data
						oldData.push(...newData)
						this.followAuthorList = oldData
					} else if (!this.newAuthorLoad) {
						this.followAuthorList = res.data
					}
					
					// 判断是否有数据
					if (res.data.length < 10) {
						return this.load[1].loading = 'noMore'
					}
				})
			},
			// 上拉刷新
			more() {
				if (this.load[0].loading === 'more') {
					this.newLoad = false
					this.load[0].page ++
					this.getFollow()
				}
				
				if (this.load[1].loading === 'more') {
					this.newAuthorLoad = false
					this.load[1].page ++
					this.getFollowAuthor()
				}
				
				console.log(2)
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
		display: flex;
	}
	.follow{
		height: 100%;
		display: flex;
		flex-direction: column;
		flex: 1;
		box-sizing: border-box;
		.follow-tab{
			height: 60upx;
			padding: 20upx 40upx;
			border-bottom: 1px solid #F5F5F5;
			.follow-tab__box{
				border: 1px solid $mk-base-color;
				border-radius: 10upx;
				display: flex;
				height: 100%;
				width: 100%;
				.follow-tab__item{
					height: 100%;
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #666;
					font-size: 28upx;
					&:first-child{
						border-right: 1px solid $mk-base-color;
					}
					&.active{
						color: $mk-base-color;
					}
				}
			}
		}
		.follow-list{
			flex: 1;
			.follow-list__swiper{
				height: 100%;
				width: 100%;
				.swiper-item{
					height: 100%;
					width: 100%;
				}
			}
		}
	}
</style>
